﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;

public partial class Admin_Control_iPad : System.Web.UI.Page
{
    string connectionString;
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand command;
    private DataSet ds = new DataSet();
    private SqlDataReader dr;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        connect = new SqlConnection(connectionString);
        command = new SqlCommand();
        if (!IsPostBack)
        {
            bindDataToDropDownIPadDetails();
        }
    } 
    protected void btnAddNewIpad_Click(object sender, EventArgs e)
    {
        DBAccess db = new DBAccess();
        if (db.CheckIDProduct(txtiPadID.Text))
        {
            lblStatus.Text = "You can't add new product because it is added before";
        }
        else
        {
            bool check = false;
            string imgipad = "", filename = "";
            if (fileUploadeiPad.HasFile)
            {
                string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
                string ext = Path.GetExtension(fileUploadeiPad.PostedFile.FileName);

                for (int i = 0; i < validate.Length; i++)
                {
                    if (ext == "." + validate[i])
                    {
                        if (fileUploadeiPad.PostedFile.ContentLength <= 512000)
                        {
                            check = true;

                            break;
                        }
                    }
                }
                if (check)
                {
                    filename = Path.GetFileName(fileUploadeiPad.FileName);
                    fileUploadeiPad.SaveAs(Server.MapPath("~/IMG/iPad/") + filename);
                    lblStatus.Visible = true;

                }
                else
                {
                    lblStatus.Text = "File must be Images and less than 500kb";
                    lblStatus.Visible = true;
                }
            }

            if (check == true)
            {
                imgipad = "~/IMG/iPad/" + filename;
                imageiPad.ImageUrl = imgipad;
            }
            else if (check == false)
            {
                lblStatus.Text = "please choose Image";
            }

            if (connect.State == ConnectionState.Open)
            {
                connect.Close();
            }
            try
            {

                if (db.CheckProduct(imgipad) || db.CheckProductDetail(imgipad))
                {
                    lbim.Text = "You can't add new picture because it is added before";
                }
                else
                {
                    connect.Open();
                    command = new SqlCommand("spInsert_iPad", connect);
                    //command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = ("spInsert_iPad '" + txtiPadID.Text + "','" + txtiPadName.Text + "','iPad','" + float.Parse(txtiPadPrice.Text) + "','" + imgipad + "'");

                    command.ExecuteNonQuery();
                    lblStatus.Text = "Insert Successfully";
                    bindDataToDropDownIPadDetails();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                connect.Close();
            }
        
        }
        
    }
    protected void bindDataToDropDownIPadDetails()
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            //ds = new DataSet();
            command = new SqlCommand("spGetProduct_iPad", connect);
            command.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(command);
            da.Fill(ds, "ProductID");
            int a = ds.Tables["ProductID"].Rows.Count;
            dropDownIPadDetails.Items.Clear();
            ddlProductID.Items.Clear();
            for (int i = 0; i < a; i++)
            {
                dropDownIPadDetails.Items.Add(ds.Tables["ProductID"].Rows[i][0].ToString());
                ddlProductID.Items.Add(ds.Tables["ProductID"].Rows[i][0].ToString());
                ddlProUp2.Items.Add(ds.Tables["ProductID"].Rows[i][0].ToString());
                ddlProDel.Items.Add(ds.Tables["ProductID"].Rows[i][0].ToString());
                ddlProDel2.Items.Add(ds.Tables["ProductID"].Rows[i][0].ToString());
            }
            
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            
            connect.Close();
        }
        
        
    }
    protected void btnAddNewDetails_Click(object sender, EventArgs e)
    {   string imgipad = "", filename = "";
        
        txtDetailsID.Text = dropDownIPadDetails.Text;

        bool check = false;
        
        if (fileUploadiPadDetails.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(fileUploadiPadDetails.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (fileUploadiPadDetails.PostedFile.ContentLength <= 512000)
                    {
                        check = true;

                        break;
                    }
                }
            }
            if (check)
            {
                filename = Path.GetFileName(fileUploadiPadDetails.FileName);
                fileUploadiPadDetails.SaveAs(Server.MapPath("~/IMG/iPad/") + filename);
                lblStatus.Visible = true;

            }
            else
            {
                lblStatus.Text = "File must be Images and less than 500kb";
                lblStatus.Visible = true;
            }
        }

        if (check == true)
        {
            imgipad = "~/IMG/iPad/" + filename;
            imageIpadDetails.ImageUrl = imgipad;
        }
        else if (check == false)
        {
            lblStatus.Text = "please choose Image";
        }

        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        try
        {
            DBAccess db = new DBAccess();
            if (db.CheckProduct(imgipad) || db.CheckProductDetail(imgipad))
            {
                lblStatus2.Text = "You can't add new picture because it is added before";
            }
            else
            {
                connect.Open();
                command = new SqlCommand("spInsert_iPadDetails", connect);
                //command.CommandType = CommandType.StoredProcedure;
                command.CommandText = ("spInsert_iPadDetails '" + txtDetailsID.Text + "','" + imgipad + "','" + txtDetailsText.Text + "'");

                command.ExecuteNonQuery();
                lblStatus2.Text = "Insert Successfully";
                bindDataToDropDownIPadDetails();
            }
            
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }

    protected void btnSelectProU_Click(object sender, EventArgs e)
    {
        ActiveUpdatePro();
        string name = ddlProductID.Text;
        if (connect.State == ConnectionState.Closed)
        {
            connect.Open();
        }
        command = new SqlCommand("spGetProductiPad_Update", connect);
        //command.CommandType = CommandType.StoredProcedure;
        command.CommandText =  (" spGetProductiPad_Update '"+ name + "'");
        da = new SqlDataAdapter(command);
        
        dr = command.ExecuteReader();
        dr.Read();
        txtNameProU.Text = dr.GetString(1).ToString();
        tbProTyU.Text = dr.GetString(2).ToString();
        tbProPrU.Text = dr.GetValue(3).ToString();
        imgProU.ImageUrl = dr.GetString(4).ToString();
    }
    public void ActiveUpdatePro()
    {
        txtNameProU.ReadOnly = false;
        
        tbProPrU.ReadOnly = false;
        FUpPro.Enabled = true;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        bool check = false;
        string imgipad = "", filename = "";
        if (FUpPro.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(FUpPro.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (FUpPro.PostedFile.ContentLength <= 512000)
                    {
                        check = true;

                        break;
                    }
                }
            }
            if (check)
            {
                filename = Path.GetFileName(FUpPro.FileName);
                FUpPro.SaveAs(Server.MapPath("~/IMG/iPad/") + filename);
                lblStatus.Visible = true;

            }
            else
            {
                lbUpdatePro.Text = "File must be Images and less than 500kb";
                lblStatus.Visible = true;
            }
        }

        if (check == true)
        {
            imgipad = "~/IMG/iPad/" + filename;
            imgProU.ImageUrl = imgipad;
        }
        else if (check == false)
        {
            lbUpdatePro.Text = "please choose Image";
        }

        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        try
        {
            DBAccess db = new DBAccess();
            if (db.CheckProduct(imgipad) || db.CheckProductDetail(imgipad))
            {
                lbUpdatePro.Text = "You can't add new picture because it is added before";
            }
            else
            {
                string name = ddlProductID.Text;
                connect.Open();
                command = new SqlCommand("spUpdateProduct_iPad", connect);
                //command.CommandType = CommandType.StoredProcedure;
                command.CommandText = ("spUpdateProduct_iPad '" + name+ "','"+ txtNameProU + "','" + float.Parse(tbProPrU.Text) + "','" + imgipad + "'");
                command.ExecuteNonQuery();
                lbUpdatePro.Text = "Update Successfully";
            }
            
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }
    protected void btnLoadProDeNoUp_Click(object sender, EventArgs e)
    {
        int no = int.Parse(ddlProDeNo.Text);
        if (connect.State == ConnectionState.Closed)
        {
            connect.Open();
        }
        command = new SqlCommand("spGetProductiPadDetailsByNo", connect);
        //command.CommandType = CommandType.StoredProcedure;
        command.CommandText = (" spGetProductiPadDetailsByNo '" + no + "'");
        da = new SqlDataAdapter(command);

        dr = command.ExecuteReader();
        dr.Read();
        tbProDeText.Text = dr.GetString(3).ToString();
        imgProDeUp.ImageUrl = dr.GetString(2).ToString();
        tbProDeText.ReadOnly = false;
        FUpProDeUp.Enabled = true;
        btnUpdateProDel.Enabled = true;
    }
   
    protected void btnSelectProDeUp_Click(object sender, EventArgs e)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            //ds = new DataSet();
            string name = ddlProUp2.Text;
            command = new SqlCommand("spGetProductiPadDetailsNoByDetaisID", connect);
            command.CommandText = ("spGetProductiPadDetailsNoByDetaisID '" + name + "'");
            da = new SqlDataAdapter(command);
            da.Fill(ds, "DetailsNo");
            int a = ds.Tables["DetailsNo"].Rows.Count;
            dropDownIPadDetails.Items.Clear();
            ddlProductID.Items.Clear();
            for (int i = 0; i < a; i++)
            {

                ddlProDeNo.Items.Add(ds.Tables["DetailsNo"].Rows[i][0].ToString());
            }

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {

            connect.Close();
        }
    }
    protected void btnUpdateProDel_Click(object sender, EventArgs e)
    {
        bool check = false;
        string imgipad = "", filename = "";
        if (FUpProDeUp.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(FUpProDeUp.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (FUpProDeUp.PostedFile.ContentLength <= 512000)
                    {
                        check = true;

                        break;
                    }
                }
            }
            if (check)
            {
                filename = Path.GetFileName(FUpProDeUp.FileName);
                FUpProDeUp.SaveAs(Server.MapPath("~/IMG/iPad/") + filename);
                lbStatusUpPro.Visible = true;

            }
            else
            {
                lbStatusUpPro.Text = "File must be Images and less than 500kb";
                lblStatus.Visible = true;
            }
        }

        if (check == true)
        {
            imgipad = "~/IMG/iPad/" + filename;
            imgProDeUp.ImageUrl = imgipad;
        }
        else if (check == false)
        {
            lbStatusUpPro.Text = "please choose Image";
        }

        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        try
        {
            if (filename == "")
            {
                imgipad = imgProDeUp.ImageUrl.ToString();
            }
            else
            {
            }
                int no = int.Parse(ddlProDeNo.Text);
                connect.Open();
                command = new SqlCommand("spUpdateProductiPadDetails", connect);
                //command.CommandType = CommandType.StoredProcedure;
                command.CommandText = ("spUpdateProductiPadDetails '" + no + "','" + imgipad + "','" + tbProDeText.Text + "'");
                command.ExecuteNonQuery();
                lbStatusUpPro.Text = "Update Successfully";
            

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }
    protected void btnSelectProDel_Click(object sender, EventArgs e)
    {
        btnDelPro.Enabled = true;
        string name = ddlProDel.Text;
        if (connect.State == ConnectionState.Closed)
        {
            connect.Open();
        }
        command = new SqlCommand("spGetProductiPad_Update", connect);
        //command.CommandType = CommandType.StoredProcedure;
        command.CommandText = (" spGetProductiPad_Update '" + name + "'");
        da = new SqlDataAdapter(command);

        dr = command.ExecuteReader();
        dr.Read();
        txtProNameDel.Text = dr.GetString(1).ToString();
        txtProTyDel.Text = dr.GetString(2).ToString();
        txtProPriDel.Text = dr.GetValue(3).ToString();
        imgProDel.ImageUrl = dr.GetString(4).ToString();
    }
    protected void btnDelPro_Click(object sender, EventArgs e)
    {
        string id = ddlProDel.Text;
        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        command = new SqlCommand("spDeleteIpad", connect);
        command.CommandText= ("spDeleteIpad '"+id+"'");
        command.ExecuteNonQuery();
        connect.Close();
    }
    protected void btnSelectProDetDel_Click(object sender, EventArgs e)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            //ds = new DataSet();
            string name = ddlProDel2.Text;
            command = new SqlCommand("spGetProductiPadDetailsNoByDetaisID", connect);
            command.CommandText = ("spGetProductiPadDetailsNoByDetaisID '" + name + "'");
            da = new SqlDataAdapter(command);
            da.Fill(ds, "DetailsNo");
            int a = ds.Tables["DetailsNo"].Rows.Count;
            ddlProDetDel.Items.Clear();
            
            for (int i = 0; i < a; i++)
            {

                ddlProDetDel.Items.Add(ds.Tables["DetailsNo"].Rows[i][0].ToString());
            }

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {

            connect.Close();
        }
    }
    protected void BtnLoadDetDel_Click(object sender, EventArgs e)
    {
        int no = int.Parse(ddlProDetDel.Text);
        if (connect.State == ConnectionState.Closed)
        {
            connect.Open();
        }
        command = new SqlCommand("spGetProductiPadDetailsByNo", connect);
        //command.CommandType = CommandType.StoredProcedure;
        command.CommandText = (" spGetProductiPadDetailsByNo '" + no + "'");
        da = new SqlDataAdapter(command);

        dr = command.ExecuteReader();
        dr.Read();
        txtTextDetDel.Text = dr.GetString(3).ToString();
        imgDetDel.ImageUrl = dr.GetString(2).ToString();
        
    }
    protected void btnDeProDet_Click(object sender, EventArgs e)
    {
        int no = int.Parse(ddlProDetDel.Text);
        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        command = new SqlCommand("spDeleteIPadDetails", connect);
        command.CommandText = (" spDeleteIPadDetails '" + no + "'");
        command.ExecuteNonQuery();
        connect.Close();
    }
}